<?php
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require 'database.php';
$db = new DB();

$url = isset($_GET['url']) ? strval($_GET['url']) : NULL;
// $url = urldecode($url);


crawl(1, $url);

function crawl($id, $url) {
	global $db;

	$embed = '';
	$sth = array(
			"'",
			'=',
			'"'
	);

	$status = get_headers($url);
	if ($status[0] == 'HTTP/1.1 404 Not Found'){
		$games = array(
				'done'=>2,
				'active'=>0
		);
		echo $url . ' <font color="red">ERROR 404 - PAGE NOT FOUND</font><br />';
	}else{
		$html = file_get_html($url);
// 		echo $html;

		// Find .swf
		$pos = strpos($html, '<object name=');
		if ($pos !== false){
			// Game
			$game_file = preg_replace('/\<!DOCTYPE html\>(.*?)\<object name=/si', '<object name=', $html);
			$game_file = preg_replace('/object\>(.*?)\<\/html\>/si', 'object>', $game_file);
			$game_file = str_replace('\\', '', $game_file);

			echo $game_file;

			$str = str_get_html($game_file);

			foreach ($str->find('object') as $element)
				$embed = $element->data;

			echo '<p><input type="text" value="' . $embed . '" style="width:800px" /></p>';
		}

		$pos = strpos($html, '<object data=');
		if ($pos !== false){
			// Game
			$game_file = preg_replace('/\<!DOCTYPE html\>(.*?)\<object data=/si', '<object data=', $html);
			$game_file = preg_replace('/object\>(.*?)\<\/html\>/si', 'object>', $game_file);
			$game_file = str_replace('\\', '', $game_file);

			echo $game_file;

			$str = str_get_html($game_file);

			foreach ($str->find('object') as $element)
				$embed = $element->data;

			echo '<p><input type="text" value="' . $embed . '" style="width:800px" /></p>';
		}

		$pos = strpos($html, '<object type=');
		if ($pos !== false){
			// Game
			$game_file = preg_replace('/\<!DOCTYPE html\>(.*?)\<object type=/si', '<object type=', $html);
			$game_file = preg_replace('/object\>(.*?)\<\/html\>/si', 'object>', $game_file);
			$game_file = str_replace('\\', '', $game_file);

			echo $game_file;

			$str = str_get_html($game_file);

			foreach ($str->find('object') as $element)
				$embed = $element->data;

			echo '<p><input type="text" value="' . $embed . '" style="width:800px" /></p>';
		}

		// Find iFrame
		$pos = strpos($html, '<iframe');
		if ($pos !== false){
			$game_file = preg_replace('/\<!DOCTYPE html\>(.*?)\<iframe/si', '<iframe', $html);
			$game_file = preg_replace('/iframe\>(.*?)\<\/html\>/si', 'iframe>', $game_file);
			$game_file = str_replace('\\', '', $game_file);

			echo $game_file;

			$str = str_get_html($game_file);

			foreach ($str->find('iframe') as $element)
				$embed = $element->src;

			echo '<p><input type="text" value="' . $embed . '" style="width:800px" /></p>';
		}
	}
}